package com.jftone.bh.app.util;

import java.util.Arrays;

import org.jftone.util.EncryptUtil;
import org.jftone.util.IData;

public class WeixinUtil {
	
	
	
	/** 
     * 验证微信公众号签名 
     *  
     * @param signature 
     * @param timestamp 
     * @param nonce 
     * @return 
	 * @throws Exception 
     */  
    public static boolean checkSignature(String signature, String timestamp, String nonce) throws Exception {   
        String tmpStr = null;  
  
        try {
            IData<String, Object> wx = WeixinConf.wx;
        	String[] arr = new String[] { wx.getString("TOKEN"), timestamp, nonce };
            // 将token、timestamp、nonce三个参数进行字典序排序  
            Arrays.sort(arr);  
            StringBuilder content = new StringBuilder();  
            for (int i = 0; i < arr.length; i++) {  
                content.append(arr[i]);  
            }
            tmpStr = EncryptUtil.sha1(content.toString());
            content = null;
        } catch (Exception e) {  
        	e.printStackTrace();
        	throw new Exception("生成验证签名错误", e);
        }   
        // 将sha1加密后的字符串可与signature对比，标识该请求来源于微信  
        return tmpStr != null ? tmpStr.toUpperCase().equals(signature.toUpperCase()) : false;  
    }  
}
